
//时间格式转换
const formatTime = date => {
  var dates = new Date(date)
  const year = dates.getFullYear()
  const month = dates.getMonth() + 1
  const day = dates.getDate()
  const hour = dates.getHours()
  const minute = dates.getMinutes()
  const second = dates.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


const ip = 'http://192.168.1.66:8080'

const fetch = (url, method, data) => {
  return new Promise(function (reslove, reject) {
    console.log(data)
      wx.request({
        url:ip + url,
      method: method,
      header:{
        'content-type': 'application/json' // 默认值
      },
      data: data,
      success: function (res) {
        console.log(11, res)
        if (res.statusCode == 510){
          
          console.log(res.data.message)
          wx.showToast({
            title: JSON.parse(res.data.message).startKm,
            icon: 'none',
            duration: 2000
          })
        }
        return reslove(res.data);
      },
      fail:function (res){
        console.log(res)
      }
    })
  })
}


const api ={
  islogin:'/driver/islogin/',
  getcode:'/driver/sendVerificationCode/app',
  login:'/driver/login/app',

  gettobedone:'/orderDetailList/app',
  sendtobedone:'/orderStatus/driverStart',
  //到达目的地
  addtravel:"/orderStatus/driverEnd  ",
  //总订单数  
  indexstar:"/orderCount/app"
}

//获取opendid
const openid = (url, method, data) => {
  return new Promise(function (reslove, reject) {
    wx.request({
      url: url,
      method: method,
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: data,
      success: function (res) {
        return reslove(res.data);
      },
      fail: function (res) {
        console.log(22,res)
      }
    })
  })
}



module.exports = {
  formatTime: formatTime,
  fetch: fetch,
  openid:openid,
  api
}
